package com.javafactroy.kunlong.bip.icp.message.dxp.base;

import com.javafactroy.kunlong.bip.icp.common.utils.IpUtils;
import com.javafactroy.kunlong.bip.icp.message.dxp.iface.IMsgHeader;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.LinkedHashMap;
import java.util.Map;

@Data
@ToString
public class DxpHeader implements IMsgHeader, Serializable {
    static Map<String, Integer> mapDefine = new LinkedHashMap<>();
    static Map<String, Object> mapValues = new LinkedHashMap<>();
    public final static int MSG_HEADER_LEN = 32;
    public final static int MAGIC_NUMBER_LENGTH = 6;
    byte[] magicNumber = {0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b};
    public DxpHeader(){
     setMsgId(IpUtils.getMsgId());
    }

    static {
        mapDefine.put("magicNumber", 0);
        mapDefine.put("versionId", 1);
        mapDefine.put("sourceId", 2);
        mapDefine.put("messageType", 3);
        mapDefine.put("msgLen", 4);
        mapDefine.put("messageId", 5);
        mapDefine.put("reserve", 6);
    }

    boolean decodeOk;
    byte[] dxpMagicNumber = new byte[MAGIC_NUMBER_LENGTH];//{0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b};

    int versionId = 1;
    int sourceId = 1;
    short msgType = 0x01;
    int msgLen;
    int msgId = 1;
    int reserve = 0x04;
     int reserve1 = 0;
     //msgState
    @Override
    public byte[] encode() {

        ByteBuffer byteBuffer = ByteBuffer.allocate(MSG_HEADER_LEN);
        byteBuffer.put(dxpMagicNumber);
        byteBuffer.putInt(versionId);
        byteBuffer.putInt(sourceId);
        byteBuffer.putShort(msgType);
        byteBuffer.putInt(msgLen);
        byteBuffer.putInt(msgId);
        byteBuffer.putInt(reserve);
        byteBuffer.putInt(reserve1);

        return byteBuffer.array();
    }

    @Override
    public void decode(byte[] buf) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
        byteBuffer.get(dxpMagicNumber);
        versionId=byteBuffer.getInt();
        sourceId=byteBuffer.getInt();
        msgType =byteBuffer.getShort();
        msgLen=byteBuffer.getInt();
        msgId =byteBuffer.getInt();
        reserve=byteBuffer.getInt();
        reserve1=byteBuffer.getInt();


    }

    @Override
    public void check() {

    }


}
